/***************************mainwindow.cpp*******************************/
/*****************************************************************************/
/*******************Browsers Daily Build Updater************************/
/************Copyright (C) 2010-2011  Gianluigi Biancucci*************/
/*****************************************************************************/
/*****This is free software, and you are welcome to redistribute it****/
/*****and / or modify underthe terms of GPLV2 License.**************/
/*****This program comes with ABSOLUTELY NO WARRANTY*****/
/*****************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "singletondownloader.h"
#include "iostream"
#include "QFile"
#include <QMessageBox>
#include <QIcon>
#include <QLabel>
#include <QDir>
#include <QProcess>
#include <QStringList>
#include "UpdaterFactory.h"
#include "firefoxupdater.h"
#include"IBrowserUpdater.h"
#include "paths.h"
#include "proxydialog.h"
#include <QProgressBar>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    process = new QProcess(this);
    QDir dir;
    dir = QDir();
    if(!dir.exists(SETTINGSPATH+"/"))dir.mkdir(SETTINGSPATH+"/");
    QObject::connect(ui->mainToolBar->addAction("Set Proxy"),SIGNAL(triggered()),this, SLOT(proxy()));
    QObject::connect(ui->mainToolBar->addAction("Help"),SIGNAL(triggered()),this, SLOT(help()));
    QObject::connect(ui->mainToolBar->addAction("About"),SIGNAL(triggered()),this, SLOT(about()));
    updater=0;




}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_checkButton_clicked(){
   if(updater==0) updater=UpdaterFactory::getInstance();
   else{
       delete updater;
       updater=UpdaterFactory::getInstance();
   }
    if(updater!=0){

        ui->checkButton->setDisabled(true);
        ui->checkButton->setText("Checking...");
        QString lastVer;
        QString installedVer;

        if(!updater->getLatestVersion(&lastVer)) {
            QMessageBox error;
            error.setText(updater->getLastError());
            error.setIcon(QMessageBox::Warning);
            error.exec();
            ui->checkButton->setText("Check now");
            ui->checkButton->setEnabled(true);
            return;
        }
        if(!updater->getInstalledVersion(&installedVer)) {
            QMessageBox error;
            error.setText(updater->getLastError());
            error.setIcon(QMessageBox::Warning);
            error.exec();
            ui->checkButton->setText("Check now");
            ui->checkButton->setEnabled(true);
            return;
        }

        if(lastVer == installedVer){
            QMessageBox msg;
            msg.setWindowTitle("Already up to date");
            msg.setIcon(QMessageBox::Information);
            msg.setText("No new version of "+updater->getBrowserName()+" since the last time I've download it for you.");
            msg.exec();
        }
        else {
            QMessageBox msg;
            msg.setWindowTitle("New version avaiable");

            if(installedVer.isEmpty()){
                msg.setText("It seems that i've never downloaded a daily build version of "+updater->getBrowserName()+" for you. Do you want to download it?");
                msg.setIcon(QMessageBox::Question);
                msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
                msg.setDefaultButton(QMessageBox::Ok);
            }else {

                msg.setText("There is a new version of "+updater->getBrowserName()+" since the last time i've download it for you. Do you want to download it?");
                msg.setIcon(QMessageBox::Question);
                msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
                msg.setDefaultButton(QMessageBox::Ok);
            }
            int c = msg.exec();
            if(c == 1024) {
                msg.setWindowTitle("Please Wait...");
                msg.setText("Downloading, please wait...");
                msg.setIcon(QMessageBox::Information);
                msg.setStandardButtons(QMessageBox::NoButton);
                msg.show();
                QString exePath;
                if(  updater->download(lastVer,&exePath)){
                    msg.close();

                    msg.setWindowTitle("Download Complete");
                    msg.setText(updater->getBrowserName()+" installer saved in: "+exePath+" .\n\n Do you want to install it right now?");
                    msg.setIcon(QMessageBox::Question);
                    msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
                    msg.setDefaultButton(QMessageBox::Cancel);

                    int c = msg.exec();
                    if(c==1024){

                        process->start(exePath);
                    }

                } else {
                    msg.close();
                    QMessageBox error;
                    error.setText(updater->getLastError());
                    error.setIcon(QMessageBox::Warning);
                    error.exec();
                }

            }
        }


        ui->checkButton->setText("Check now");
        ui->checkButton->setEnabled(true);
    }else{
        QMessageBox error;
        error.setText("Select a Browser!");
        error.setIcon(QMessageBox::Warning);
        error.exec();
    }
}

void MainWindow::on_radio32_clicked(){    
    UpdaterFactory::setArch("32");
}

void MainWindow::on_radio64_clicked(){
    UpdaterFactory::setArch("64");
}

void MainWindow::on_firefoxradio_clicked(){
    UpdaterFactory::setUpdater(ui->firefoxradio->text());
    ui->radio32->setEnabled(true);
    ui->radio64->setEnabled(true);
}

void MainWindow::on_chromiumradio_clicked(){
    UpdaterFactory::setUpdater(ui->chromiumradio->text());
    ui->radio64->setEnabled(false);
    ui->radio32->setChecked(true);
}

void MainWindow::on_operaradio_clicked(){
    UpdaterFactory::setUpdater(ui->operaradio->text());
    ui->radio64->setEnabled(false);
    ui->radio32->setChecked(true);
}

void MainWindow::proxy(){

    ProxyDialog* dialog= new ProxyDialog(this);
    dialog->show();

}

void MainWindow::help(){
    QMessageBox msg;
    msg.setWindowTitle("About BDBU.");
    msg.setIcon(QMessageBox::Information);
    QString strMsg="This simple program will download for you the latest daily build for the selected browser.\n\n";
    strMsg+="Sometimes these builds are very unstable and you choose to use it at your own risk.\n";
    strMsg+="This program, at this time of development, checks if there is a newer browser version than the last one it downloaded: still not\n";
    strMsg+="recognize the installed version, so if you update your browser outside\nthis program, it can't notice the update.I'm working on this.";
    msg.setText(strMsg);
    msg.exec();
}

void MainWindow::about(){
    QMessageBox msg;
    msg.setWindowTitle("About BDBU.");
    msg.setIcon(QMessageBox::Information);
    QString strMsg="Created by Gianluigi Biancucci\n";
    strMsg+="gianluigi85@gmail.com\n\n";
    strMsg+="This program is Free software\n";
    strMsg+="and is distribuited WITHOUT ANY WARRANTY.\n";
    strMsg+="You can redistribuite it and / or modify it\n";
    strMsg+="under the terms and condition of GPLV2 license.\n\n";
    strMsg+="For more information see:\nhttp://www.gnu.org/licenses";
    msg.setText(strMsg);
    msg.exec();
}

